package cn.itcast.mq.email.listener;


import cn.itcast.util.JedisUtils;
import cn.itcast.util.SmsCodeUtil;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import redis.clients.jedis.Jedis;


public class MySmsCodeListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
        String phone = new String(body).replace("\"","");
        //System.out.println("手机号"+phone);
        try {
            String code = SmsCodeUtil.sendSms(phone);

            //将code存到redis中
            Jedis jedis = JedisUtils.getJedis();
            jedis.setex("registerCode_"+phone,60,code);
            jedis.close();

            System.out.println("验证码为:"+code);
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println("短信发送失败!");
        }

    }
}
